package com;

import com.utils.FormatControllerUtils;

public class ByteMain {


    public static void main(String[] args) {
//        byte[] data = new byte[]{(byte)0x55, (byte) 0xAA, 0x00, 0x07, (byte) 0x25, 0x01,0x2C};
//        byte value = (byte) (data[0] + data[1] + data[2] + data[3] + data[4] + data[5]);
//        int a = 0;
//        a = 0;
//
//        byte ss = 0;
//        for (int i = 0; i < 6; i++) {
//            ss += data[i];
//        }
//
//        a = 0;

        //pushLongtoBySize(Long.MAX_VALUE, 9);

        byte[] data = new byte[]{
                0x55, (byte) 0xAA, 0x00, 0x19, 0x25,
                0x41, 0x32,
                0x3A, 0x42,
                0x36, 0x3A,
                0x33, 0x36, 0x3A,
                0x38, 0x39, 0x3A,
                0x30, 0x30, 0x3A,
                0x45, 0x38, 0x00, 0x01, 0x00

        };

        int value = getResultProtocol(data);
        int a = 0;
        a = 0;

        System.out.println(FormatControllerUtils.printHexBinary(data));


    }

    public static int getResultProtocol(byte[] data) {
        int resultProtocol = 0;
        int length = 0;
        if (data.length >= 5) {
            if ((data[0] & 0xff) == 0x55 && (data[1] & 0xff) == 0xAA) {
                length = ((data[2] & 0xff) << 8) + (data[3] & 0xff);
                if (data.length == length) {
                    byte valueByte = 0;
                    for (int i = 0; i < length - 2; i++) {
                        valueByte += data[i];
                    }
                    if (valueByte == data[length - 2]) {
                        resultProtocol = data[4] & 0xff;
                        System.out.println("OK");
                    }
                }
            }
        }
        return resultProtocol;
    }

    public static byte[] pushLongtoBySize(long value, int size) {
        byte[] byteValue = new byte[size];
        String valueContent = String.valueOf(value);

        if (Long.MAX_VALUE == value) {
            for (int i = 0; i < size; i++) {
                byteValue[i] = (byte) 0xFF;
                //pushByte(byteValue[i]);
            }

        } else {

            for (int i = size - 1; i >= 0; i--) {
                int valueLength = valueContent.length();
                if (valueLength > 0) {
                    String per = valueContent.substring(valueLength - 1);
                    valueContent = valueContent.substring(0, valueLength - 1);
                    byteValue[i] = Byte.valueOf(per);
                } else {
                    byteValue[i] = 0x00;
                }
            }
            for (int i = 0; i < size; i++) {
                //pushByte(byteValue[i]);
            }
        }


        return byteValue;
    }
}
